package ${basePackage}.generator;

import ${basePackage}.generator.StaticGenerator;
import ${basePackage}.generator.DynamicGenerator;
import ${basePackage}.model.DataModel;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.IOException;


<#macro generateFile indent fileInfo>
${indent}inputPath = new File(inputRootPath, "${fileInfo.inputPath}").getAbsolutePath();
${indent}outputPath = new File(outputRootPath, "${fileInfo.outputPath}").getAbsolutePath();
<#if fileInfo.generateType == "static">
${indent}StaticGenerator.copyFilesByHutool(inputPath, outputPath);
<#else >
${indent}DynamicGenerator.doGenerate(inputPath, outputPath, model);
</#if>
</#macro>

public class MainGenerator {

/**
* 生成
*
* @param model 数据模型
* @throws TemplateException
* @throws IOException
*/
public static void doGenerate(DataModel model) throws TemplateException, IOException {
String inputRootPath = "${fileConfig.inputRootPath}";
String outputRootPath = "${fileConfig.outputRootPath}";

String inputPath;
String outputPath;


<#list modelConfig.models as modelInfo>
<#-- 有分组 -->
    <#if modelInfo.groupKey??>
        <#list modelInfo.models as subModelInfo>
            ${subModelInfo.type} ${subModelInfo.fieldName} = model.${modelInfo.groupKey}.${subModelInfo.fieldName};
        </#list>
    <#else>
        ${modelInfo.type} ${modelInfo.fieldName} = model.${modelInfo.fieldName};
    </#if>
</#list>
<#list fileConfig.files as fileInfo>
    // 1.先判断是否为分组
    <#if fileInfo.groupKey??>
        // 2.分组下是否有condition
        <#if fileInfo.condition??>
            if (${fileInfo.condition}) {
            <#list fileInfo.files as fileInfo>
                // 3.本组文件循环
                <@generateFile indent="            " fileInfo=fileInfo />
            </#list>
            }
        <#else>
            <@generateFile indent="        " fileInfo=fileInfo />
        </#if>
        // 这里是分组的else，复用单个文件的判断
    <#else>
        <#if fileInfo.condition??>
            if (${fileInfo.condition}) {
            <@generateFile indent="            " fileInfo=fileInfo />
            }
        <#else>
            <@generateFile indent="        " fileInfo=fileInfo />
        </#if>
    </#if>
</#list>
}
}










